/**
 * 常用shader
 * @author今天铲子了吗
 * @host www.wellyyss.cn
 * @qq group 169470811
 */
import _3d from '../../_3d.js'





// 模型shader
const _modelShader = function () {
    this.uniforms = {
        //主体渐变色
        gradualColorStart:{ value: new _3d.Color('#001327')},
        gradualColorEnd:{ value: new _3d.Color('#00FFFF')},
        gradualPositionYStart: { value: 0}, //纵向光带初始渐变高度 --smoothstep的参数1
        gradualPositionYEnd: { value: 20},//纵向光带结束渐变高度--smoothstep的参数2

        //纵向光线
        rowLightHeight: { value:  0},//纵向光的变化高度
        rowLightWidth: { value: 10},// 纵向光宽
        selfTexture: {}, //原贴图纹理
        rowLightColor:{ value: new _3d.Color('#88FF69')},

        //shader透明度占比
        shaderOpacity: { value: 0.9},//

        //横向线
        colLineZ: { value: -200},//定义横向线的 z
        colLineWidth: { value: 10},//定义横向线的 宽度
        colLineColor: { value: new _3d.Color('#b017ff')}, //横向线颜色

        //圆圈扫描
        circleCenter: { value: new _3d.Vector3(0,0,0)}, //扫描中心点
        circleMinR: { value:0 }, //初始半径
        circleWidth: { value: 20 }, //圆环宽度
        colorCircle:{ value: new _3d.Color('#b017ff')},

    }
    this.vs = `
        varying vec2 vUv;
        varying vec3 v_p;
        void main() {
            vUv = uv;
            v_p = position;
            gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
         } `
    this.fs =` // 根据片元的高度来渐变
        const float PI = 3.141592654;
        uniform float rowLightHeight;
        uniform float rowLightWidth;
        uniform float shaderOpacity;
        uniform float colLineZ;
        uniform float colLineWidth;
        uniform float gradualPositionYStart;
        uniform float gradualPositionYEnd;
        uniform sampler2D selfTexture;
        uniform vec3 gradualColorStart;
        uniform vec3 gradualColorEnd;
        uniform vec3 rowLightColor;
        uniform vec3 colLineColor;
        uniform vec3 colorCircle; 
        uniform vec3 circleCenter;
        uniform float circleMinR;
        uniform float circleWidth;
        varying vec2 vUv;
        varying vec3 v_p;
        float getLeng(float x, float z){
            return  sqrt((x-circleCenter.x)*(x-circleCenter.x)+(z-circleCenter.z)*(z-circleCenter.z)); //sqrt开根号 取R
        }
      
        float plot (vec2 st, float pct){
          return  smoothstep( pct-rowLightWidth, pct, v_p.y) - smoothstep( pct, pct+0.02, v_p.y);
        }
        void main(){
        float f1 = plot(vUv,rowLightHeight);
        vec4 b1 = vec4(rowLightColor.r, rowLightColor.g, rowLightColor.b, 1.0) ;
        // vec3 gradient = mix(gradualColorStart, gradualColorStart, v_p.y * 0.1); // 除法渐变 0.1 或者说 10.0 是指停止渐变高度
        vec3 gradient =  mix(gradualColorStart, gradualColorEnd, smoothstep( gradualPositionYStart, gradualPositionYEnd, v_p.y)); //内置 smoothstep法渐变
        // gl_FragColor = vec4(gradient,1.); // 仅仅渐变色
        gl_FragColor = mix(vec4(gradient,1.),b1,f1);  //渐变与光效混合
        gl_FragColor = mix(texture2D(selfTexture, vUv),vec4(gl_FragColor.r,gl_FragColor.g,gl_FragColor.b,shaderOpacity),0.5);  //再混合材质
        if( abs(v_p.z - colLineZ) <= colLineWidth ) { // 如果在扫描线范围内
           gl_FragColor = mix(vec4(gl_FragColor.r,gl_FragColor.g,gl_FragColor.b,1), vec4(colLineColor.r,colLineColor.g,colLineColor.b,(1.0- abs(v_p.z - colLineZ) / colLineWidth) * 0.9), 0.5);
        }
        float cr = getLeng(v_p.x,v_p.z);
        if (abs(cr - circleMinR) <= circleWidth ) {
          gl_FragColor = mix(
          vec4(gl_FragColor.r,gl_FragColor.g,gl_FragColor.b,1),
          vec4(colorCircle.r,colorCircle.g,colorCircle.b,(1.0- abs(cr - circleMinR) / circleWidth) * 0.9),
          0.5);
        }
       } `
}
const modelShader = () => new _modelShader()


export  {
    modelShader
}